//
//  PartnerRulesVC.swift
//  zhonglb_iOS
//
//  Created by lxm on 2022/7/25.
//

import UIKit
import HandyJSON

class PartnerRulesVC: ZMViewController {

    //MARK:--------------------属性--------------------
//    var infos = [PartnerInfo]() {
//        didSet {
//            if infos.count > 0 {
//                areaOne.text = "必须满足以下条件方可申请"
//                areaTwo.text = "1、超级会员身份以上可申请；"
//            }
//            
//            if infos.count > 1 {
//                cityOne.text = "必须满足以下条件方可申请"
//                cityTwo.text = "1、超级会员身份以上可申请；"
//            }
//        }
//    }
    

    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var qyhhrBG: UIImageView!
    
    @IBOutlet weak var sjhhrBG: UIImageView!
    
    @IBOutlet weak var qyBtn: UIButton!
    
    @IBOutlet weak var sjBtn: UIButton!
        
    @IBOutlet weak var areaOne: UILabel!
    
    @IBOutlet weak var areaTwo: UILabel!
    
    @IBOutlet weak var cityOne: UILabel!
    
    @IBOutlet weak var cityTwo: UILabel!
        
    var loading = XMLoadingView()
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        loading.showInView(fatherView: view)
        getInfo()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = "合伙人"
                
        qyBtn.style.GradientLeftToRight(left: "#F7CC97".zm.hex(), right: "#D8A962".zm.hex())
        
        sjBtn.style.GradientLeftToRight(left: "#F7CC97".zm.hex(), right: "#D8A962".zm.hex())
        if AppHelper.appVerify() {
            areaTwo.text = "必须满足以下条件方可申请：\n1、服务商V1身份以上可申请；\n2、申请区域必须是自己注册所在地；"
        } else {
            areaTwo.text = "必须满足以下条件方可申请：\n1、服务商V1身份以上可申请；\n2、直推≥100人，间推≥1000人；\n3、申请区域必须是自己注册所在地；"
        }
    }
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        
    }
        
    override func rightBtnClick(sender: UIButton) {

    }
    
    @IBAction func onExchange(_ sender: UIButton) {
        let type = sender.tag - 100
        ZMToast.showActivity()
        PartnerAPI.apply(["agentLevel" : type + 1]).rxRequest(type: Bool.self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.showToast(title: "已经提交申请")
            self.getInfo()
        }).disposed(by: disposeBag)
    }
    
    
    func getInfo() {
        PartnerAPI.info.rxRequest(type: PartnerModels.InfoModel.self).subscribe(onNext:{ [unowned self] resp in
            self.loading.hideView(anmation: true)
            if resp.state == 0 {
                // 审核中
                self.qyBtn.style.GradientLeftToRight(left:Style.disableColor , right:Style.disableColor)
                self.sjBtn.style.GradientLeftToRight(left:Style.disableColor , right:Style.disableColor)
                self.qyBtn.isEnabled = false
                self.sjBtn.isEnabled = false
                
                self.qyBtn.setTitle(resp.agentLevel == 2 ? "已申请" : "立即申请", for: .normal)
                self.sjBtn.setTitle(resp.agentLevel == 1 ? "已申请" : "立即申请", for: .normal)
            } else if resp.state == 1 {
                // 通过
                self.qyBtn.style.GradientLeftToRight(left:Style.disableColor , right:Style.disableColor)
                self.sjBtn.style.GradientLeftToRight(left:Style.disableColor , right:Style.disableColor)
                self.qyBtn.isEnabled = false
                self.sjBtn.isEnabled = false
                
                self.qyBtn.setTitle(resp.agentLevel == 2 ? "已是合伙人" : "立即申请", for: .normal)
                self.sjBtn.setTitle(resp.agentLevel == 1 ? "已是合伙人" : "立即申请", for: .normal)
            } else if resp.state == 2 || resp.state == nil {
                // 拒绝 未通过
                qyBtn.style.GradientLeftToRight(left: "#F7CC97".zm.hex(), right: "#D8A962".zm.hex())
                sjBtn.style.GradientLeftToRight(left: "#F7CC97".zm.hex(), right: "#D8A962".zm.hex())
                self.qyBtn.isEnabled = true
                self.sjBtn.isEnabled = true
                self.qyBtn.setTitle("立即申请", for: .normal)
                self.sjBtn.setTitle("立即申请", for: .normal)
            }
            
        }, onError: { e in
            self.loading.showError(e.localizedDescription)
        }).disposed(by: disposeBag)
    }
    //MARK:--------------------delegate--------------------

}

class PartnerInfo : HandyJSON {
    required init(){}
    var income = 0.0
    var payRice = 0.0
    // 1省，2市， 3区
    var agentLevel = 0
}
